package com.ruoyi.system.service.impl;

import java.util.List;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DefNoticeMapper;
import com.ruoyi.system.domain.DefNotice;
import com.ruoyi.system.service.IDefNoticeService;
import org.springframework.util.CollectionUtils;


/**
 * 公告管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class DefNoticeServiceImpl implements IDefNoticeService 
{
    private static final Logger log = LoggerFactory.getLogger(DefNoticeServiceImpl.class);

    @Autowired
    private DefNoticeMapper defNoticeMapper;

    /**
     * 查询公告管理
     * 
     * @param id 公告管理主键
     * @return 公告管理
     */
    @Override
    public DefNotice selectDefNoticeById(Long id)
    {
        DefNotice defNotice = defNoticeMapper.selectDefNoticeById(id);
        try {
            if (StringUtils.isNotEmpty(defNotice.getFiles())) {
                defNotice.setFileList(new JsonMapper().readValue(defNotice.getFiles(), List.class));
            }
        } catch (Exception e) {
            log.error("文件列表解析异常", e);
        }
        return defNotice;
    }

    /**
     * 查询公告管理列表
     * 
     * @param defNotice 公告管理
     * @return 公告管理
     */
    @Override
    public List<DefNotice> selectDefNoticeList(DefNotice defNotice)
    {
        return defNoticeMapper.selectDefNoticeList(defNotice);
    }

    /**
     * 新增公告管理
     * 
     * @param defNotice 公告管理
     * @return 结果
     */
    @Override
    public int insertDefNotice(DefNotice defNotice)
    {
        defNotice.setCreateTime(DateUtils.getNowDate());
        defNotice.setUpdateTime(DateUtils.getNowDate());
        return defNoticeMapper.insertDefNotice(defNotice);
    }

    @Override
    public Boolean addNotice(DefNotice defNotice)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        defNotice.setCreateBy(loginUser.getUsername());
        defNotice.setUpdateBy(loginUser.getUsername());
        defNotice.setDrillId(Long.valueOf(loginUser.getDrillId()));
        if (!CollectionUtils.isEmpty(defNotice.getFileList())) {
            try {
                defNotice.setFiles(new JsonMapper().writeValueAsString(defNotice.getFileList()));
            } catch (Exception e) {
                log.error("文件列表解析异常", e);
            }
        }
        return insertDefNotice(defNotice) > 0;
    }

    /**
     * 修改公告管理
     * 
     * @param defNotice 公告管理
     * @return 结果
     */
    @Override
    public int updateDefNotice(DefNotice defNotice)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        defNotice.setUpdateBy(loginUser.getUsername());
        defNotice.setUpdateTime(DateUtils.getNowDate());
        return defNoticeMapper.updateDefNotice(defNotice);
    }

    /**
     * 批量删除公告管理
     * 
     * @param ids 需要删除的公告管理主键
     * @return 结果
     */
    @Override
    public int deleteDefNoticeByIds(Long[] ids)
    {
        return defNoticeMapper.deleteDefNoticeByIds(ids);
    }

    /**
     * 删除公告管理信息
     * 
     * @param id 公告管理主键
     * @return 结果
     */
    @Override
    public int deleteDefNoticeById(Long id)
    {
        return defNoticeMapper.deleteDefNoticeById(id);
    }
}
